import re
import inspect
from src.core import *
from typing import List
from src.mod import Wfs
from src.db import dbm, dbs
from typing import Annotated
from pydantic import BaseModel
from src.core.variables.constant import *
from pydantic import Field, AfterValidator


class _Validate:
    @staticmethod
    def on_empty(v: str) -> str:
        if not v.strip():
            if v not in [""]:
                log.ERROR(f"字段值不能为空: {v}")
                raise ValueError(f"字段值不能为空")
        return v

    @staticmethod
    def on_method(v: str) -> str:
        methods = [
            name
            for name, obj in inspect.getmembers(Wfs)
            if inspect.ismethod(obj)
            or inspect.isfunction(obj)
            and not name.startswith("__")  # 排除 __xxx__ 方法
        ]
        if v not in methods:
            log.ERROR(f"功能不存在: {v}")
            raise ValueError(f"功能不存在！")
        return v


class Wfs_schema(BaseModel):
    """添加角色"""

    path: Annotated[str, AfterValidator(_Validate.on_empty)] = Field(
        ..., description="绝对路径"
    )
    method: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.on_method)
    ] = Field(..., description="绝对路径")
